using System;
using System.IO;
using System.Runtime.InteropServices;

namespace App.Desk.CameraHelper.HK
{
	public class CHCNetSDK
	{
		public delegate void REALDATACALLBACK(int lRealHandle, uint dwDataType, ref byte pBuffer, uint dwBufSize, IntPtr pUser);

		public struct NET_DVR_DEVICEINFO_V30
		{
			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 48, ArraySubType = UnmanagedType.I1)]
			public byte[] sSerialNumber;

			public byte byAlarmInPortNum;

			public byte byAlarmOutPortNum;

			public byte byDiskNum;

			public byte byte_0;

			public byte byChanNum;

			public byte byStartChan;

			public byte byAudioChanNum;

			public byte byte_1;

			public byte byZeroChanNum;

			public byte byMainProto;

			public byte bySubProto;

			public byte bySupport;

			public byte bySupport1;

			public byte bySupport2;

			public ushort wDevType;

			public byte bySupport3;

			public byte byMultiStreamProto;

			public byte byStartDChan;

			public byte byStartDTalkChan;

			public byte byHighDChanNum;

			public byte bySupport4;

			public byte byLanguageType;

			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9, ArraySubType = UnmanagedType.I1)]
			public byte[] byRes2;
		}

		public struct NET_DVR_PREVIEWINFO
		{
			public int lChannel;

			public uint dwStreamType;

			public uint dwLinkMode;

			public IntPtr hPlayWnd;

			public bool bBlocked;

			public bool bPassbackRecord;

			public byte byPreviewMode;

			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
			public byte[] byStreamID;

			public byte byProtoType;

			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 222, ArraySubType = UnmanagedType.I1)]
			public byte[] byRes;
		}

		public struct NET_DVR_JPEGPARA
		{
			public ushort wPicSize;

			public ushort wPicQuality;
		}

		public const int MAX_ID_COUNT = 256;

		public const int MAX_STREAM_ID_COUNT = 1024;

		public const int STREAM_ID_LEN = 32;

		public const int PLAN_ID_LEN = 32;

		public const int SERIALNO_LEN = 48;

		[DllImport("HCNetSDK.dll")]
		public static extern bool NET_DVR_Init();

		[DllImport("HCNetSDK.dll")]
		public static extern int NET_DVR_Login_V30(string sDVRIP, int wDVRPort, string sUserName, string sPassword, ref CHCNetSDK.NET_DVR_DEVICEINFO_V30 lpDeviceInfo);

		[DllImport("HCNetSDK.dll")]
		public static extern uint NET_DVR_GetLastError();

		[DllImport("HCNetSDK.dll")]
		public static extern bool NET_DVR_Logout(int iUserID);

		[DllImport("HCNetSDK.dll")]
		public static extern int NET_DVR_RealPlay_V40(int iUserID, ref CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo, CHCNetSDK.REALDATACALLBACK fRealDataCallBack_V30, IntPtr pUser);

		[DllImport("HCNetSDK.dll")]
		public static extern bool NET_DVR_CaptureJPEGPicture_NEW(int lUserID, int lChannel, ref CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara, byte[] sJpegPicBuffer, uint dwPicSize, ref uint lpSizeReturned);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CaptureJPEGPicture(int lUserID, int lChannel, ref CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara, string sPicFileName);

      

        public static bool CaptureJPEGPicture(int LoginID, int Channel, ref CHCNetSDK.NET_DVR_JPEGPARA jpegPara, out MemoryStream stream)
		{
			byte[] array = new byte[1048576];
			uint num = 0u;
			bool flag = CHCNetSDK.NET_DVR_CaptureJPEGPicture_NEW(LoginID, Channel, ref jpegPara, array, 1048576u, ref num);
			stream = new MemoryStream();
			if (flag)
			{
				byte[] array2 = new byte[num];
				Array.Copy(array, 0L, array2, 0L, (long)((ulong)num));
				stream = new MemoryStream(array2);
			}
			return flag;
		}

		public CHCNetSDK()
		{
		
			
		}
	}
}
